<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>后台管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="/static/back/css/font.css">
    <link rel="stylesheet" href="/static/back/css/xadmin.css">
    <script type="text/javascript" src="/static/client/js/jquery-1.12.4.min.js"></script>
    <script type="text/javascript" src="/static/back/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/static/common/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form" onsubmit="return false">
            <div class="layui-form-item">
                <label for="goodname" class="layui-form-label">
                    <span class="x-red">*</span>商品名称
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="goodname" name="goodName"  required=""
                           lay-verify="required"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">商品图片</label>
                <div class="layui-input-inline">
                    <input type="text" name="imgUrl" lay-verify="required" id="inputimgurl" placeholder="图片地址" value="/static/back/images/404.jpg" class="layui-input">
                </div>
                <div class="layui-input-inline">
                    <div class="layui-upload-list" style="margin:0">
                        <img height="150" width="150" src="/static/back/images/404.jpg" id="srcimgurl" class="layui-upload-img">
                    </div>
                </div>
                <div class="layui-input-inline layui-btn-container" style="width: auto;">
                    <button type="button" class="layui-btn layui-btn-primary" id="editimg">修改图片</button>
                </div>
                <div class="layui-form-mid layui-word-aux">图片大小在50kb以内</div>
            </div>
            <div class="layui-form-item">
                <label for="price" class="layui-form-label">
                    <span class="x-red">*</span>商品价格
                </label>
                <div class="layui-input-inline">
                    <input type="number" id="price" name="price"  required=""
                           lay-verify="required"
                           autocomplete="off" class="layui-input"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="unit" class="layui-form-label">
                    <span class="x-red">*</span>商品单位
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="unit" name="unit"  required=""
                           lay-verify="required"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><span class="x-red">*</span>商品分类</label>
                <div class="layui-input-block" style="height: 10px;">
                    <select name="typeId">
                        <option th:each="type : ${typeList}" th:text="${type.typeName}" th:value="${type.id}" ></option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label for="intro" class="layui-form-label">
                    描述
                </label>
                <div class="layui-input-block">
                    <textarea placeholder="请输入内容" id="intro" name="intro" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"></label>
                <button class="layui-btn" lay-filter="add" lay-submit="">
                    添加
                </button>
            </div>
        </form>
    </div>
</div>
<script>
    layui.use(['form', 'layer'],
    function () {
        $ = layui.jquery;
        var form = layui.form,
            layer = layui.layer;

        layui.config({
            base: '/static/common/js/' //layui自定义layui组件目录
        }).use(['form','croppers'], function () {
            var   croppers = layui.croppers;
            //创建一个上传图片组件
            croppers.render({
                elem: '#editimg'
                ,saveW:200     //保存宽度
                ,saveH:200
                ,mark:1/1    //选取比例
                ,area:'800px'  //弹窗宽度
                ,url: "fileUpload"  //图片上传接口返回和（layui 的upload 模块）返回的JOSN一样
                ,done: function(src){ //上传完毕回调
                    console.log(src);
                    $("#inputimgurl").val(src);
                    $("#srcimgurl").attr('src',src);
                }
            });
        });

        //监听提交
        form.on('submit(add)',
            function (data) {
                console.log(data);
                //发异步，把数据提交给Control
                $.ajax({
                    url: "save",
                    method: "post",
                    dataType: "json",
                    data: data.field,
                    success: function (result) {
                        console.log(result);
                        if (result.code === 200) {
                            layer.alert("添加成功", {
                                    icon: 6
                                },
                                function () {
                                    //关闭当前frame
                                    xadmin.close();
                                    // 可以对父窗口进行刷新
                                    xadmin.father_reload();
                                });
                        } else {
                            layer.alert(result.msg)
                        }
                    },
                    error: function () {
                        layer.alert("程序出错")
                    }
                });
                return false;
            });
    });
</script>
</body>

</html>

